package com.blogs;

import com.blogs.enums.TurnstileEvents;
import com.blogs.enums.TurnstileStates;
import com.blogs.meachine.StatemachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.statemachine.StateMachine;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: xiaokai
 * @Description: 状态机启动测试类
 * @Date: 2019/5/28
 * @Version: 1.0
 */
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class SpringStateMachineApplication implements CommandLineRunner {

    @Autowired
    private StatemachineService statemachineService;

    /*@Autowired
    public StateMachine<TurnstileStates, TurnstileEvents> stateMachine;*/

    public static void main(String[] args) {
        SpringApplication.run(SpringStateMachineApplication.class);
    }

    /**
     * 测试启动自定义状态机
     *
     * @param strings
     * @throws Exception
     */
    @Override
    public void run(String... strings) throws Exception {

        Map<String, Object> context = new HashMap<>(16);
        context.put("context", "some code");
        statemachineService.execute(1, TurnstileEvents.PUSH, context);
        statemachineService.execute(1, TurnstileEvents.PUSH, context);
        statemachineService.execute(1, TurnstileEvents.COIN, context);
        statemachineService.execute(1, TurnstileEvents.COIN, context);
//        stateMachine.start();
//        System.out.println("--- coin ---");
//        stateMachine.sendEvent(TurnstileEvents.COIN);
//        System.out.println("--- coin ---");
////        stateMachine.sendEvent(TurnstileEvents.COIN);
//        System.out.println("--- push ---");
//        stateMachine.sendEvent(TurnstileEvents.PUSH);
//        System.out.println("--- push ---");
////        stateMachine.sendEvent(TurnstileEvents.PUSH);
//        stateMachine.stop();

    }
}
